package com.vital.service;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class LocationService implements LocationListener {
	
	private final Context mContext;
	
	//---flag for GPS status---
	boolean isGPSEnabled = false;
	//---flag for Network status
	boolean isNetworkEnabled = false;
	boolean canGetLocation = false;
	Location location;
	private double lat;
	private double lng;
	private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
	private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
	
	protected LocationManager locationManager;
	
	public LocationService(Context ctx) {
		this.mContext = ctx;
		getLocation();
	}
	
	public Location getLocation() {
		try {
			locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
			
			//---Getting GPS status---
			isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			
			//---Getting Network status---
			isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			//isNetworkEnabled = false;
			if (!isGPSEnabled && !isNetworkEnabled) {
				this.canGetLocation = false;
			} else {
				this.canGetLocation = true;
				//---First get location from Network Provider---
				if (isNetworkEnabled) {
					locationManager.requestLocationUpdates(
							LocationManager.NETWORK_PROVIDER, 
							MIN_TIME_BW_UPDATES,
							MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					
					if (locationManager != null) {
						location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (location != null) {
							lat = location.getLatitude();
							lng = location.getLongitude();
							setLat(lat);
							setLng(lng);
						}
					}
				} 
				if (isGPSEnabled) {
					locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
							MIN_TIME_BW_UPDATES,
							MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					if (locationManager != null) {
						location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						if (location != null) {
							lat = location.getLatitude();
							lng = location.getLongitude();
							setLat(lat);
							setLng(lng);
						}
						
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			this.canGetLocation = false;
		}
		//Toast.makeText(mContext, "Latitute begin: "+lat+"-Longtitute begin: "+lng, Toast.LENGTH_LONG).show();
		return location;
	}
	
	public void stopUsingGPS() {
		if (locationManager != null) {
			locationManager.removeUpdates(LocationService.this);
		}
	}
	
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null) {
			Log.d("LOCATION_CHANGED", location.getLatitude() + "");
			Log.d("LOCATION_CHANGED", location.getLongitude() + "");
			
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			setLat(lat);
			setLng(lng);
			
			//Toast.makeText(mContext.getApplicationContext(), "Latitute: "+this.lat+" - Longtitute:"+this.lng, Toast.LENGTH_LONG).show();
		}
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		Toast.makeText(mContext.getApplicationContext(), "GPS disabled", Toast.LENGTH_LONG).show();
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		Toast.makeText(mContext.getApplicationContext(), "GPS enabled", Toast.LENGTH_LONG).show();
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	public boolean canGetLocation() {
		return this.canGetLocation;
	}
	
	
	public void showSettingAlert() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
		alertDialog.setTitle("GPS Settings");
		alertDialog.setMessage("GPS is not enabled. Do you want to enable this");
		
		alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				mContext.startActivity(intent);
			}
		});
		
		alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		
		alertDialog.show();
	}
	
	public double getLat() {
//		if (location != null) {
//			lat = location.getLatitude();
//		}
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLng() {
//		if (location != null) {
//			lng = location.getLongitude();
//		}
		return lng;
	}

	public void setLng(double lng) {
		
		this.lng = lng;
	}

}
